package com.astrocsr.dao;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.domain.bean.TblUserReport;

@Repository("userReportDAO")
public class UserReportDAO extends BaseHelperDAO {

	public static final String ID 			= "userReportId";
	public static final String EMAIL 		= "email";

	public List<TblUserReport> findByEmail(Object email) throws Exception {
		return findByProperty(EMAIL, email);
	}
	
	public TblUserReport findById(Object email) throws Exception {
		List<TblUserReport> list = findByProperty(ID, email);
		TblUserReport tblUserReport = null;
		
		if (list != null && list.size() > 0) {
			tblUserReport = list.get(0);
		}
		
		return tblUserReport;
	}

	@SuppressWarnings("unchecked")
	public List<TblUserReport> findByProperty(String propertyName, Object value) throws Exception {

		try {

			String queryString = "from TblUserReport as model where model." + propertyName + "= :propertyName";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter("propertyName", value);
			return query.list();

		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TblUserReport> listUserReport(String email) throws Exception {
		
		String queryString = "from TblUserReport "; // Hibernate Query Language
		if (StringUtils.isNotBlank(email)) {
			queryString += " where email = :email";
		}

		Query query = getCurrentSession().createQuery(queryString); 

		if (StringUtils.isNotBlank(email)) {
			query.setParameter("email", email);
		}
		
		return query.list();
	}
	
	/**
	 * Insert customer into database
	 */
	public void addUserReport(TblUserReport entity) throws Exception {
		// Insert into database
		getCurrentSession().save(entity);
	}
}